<?php
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

//Set properties
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(0)->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(1)->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(2)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(3)->setWidth(20);

$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(23);
$objPHPExcel->getActiveSheet()->getStyle('A3:D3')->getFill()->getStartColor()->setARGB('dadada');
$objPHPExcel->getActiveSheet()->getStyle('A3:D3')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('A3:D3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);

// Add some data
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setName('Times New Roman');
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:D1')->setCellValue("A1","Karakteristik Responden");
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');

$objPHPExcel->setActiveSheetIndex(0)
            ->mergeCells('A3:A4')->setCellValue('A3', 'No')
			->mergeCells('B3:C4')->setCellValue('B3','Karakteristik')
            ->mergeCells('D3:D4')->setCellValue('D3', 'Prosentase');

/********************Jenis Kelamin*******************/
$a = 4;
$objPHPExcel->setActiveSheetIndex(0)
		    ->mergeCells('A5:A6')->setCellValue('A5', "1")
		    ->mergeCells('B5:B6')->setCellValue('B5', "Jenis Kelamin");
			$count_l = 0;
			$count_p = 0;
		    foreach ($laporan_jk as $row) {
		    	$a++;
		    	($row["jenis_kelamin"] == "L") ? $count_l++ : $count_p++ ;
		    }
			
			$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('C5', "Laki - Laki")
							->setCellValue('D5', round(($count_l/count($laporan_jk)*100),2) . "%")
							->setCellValue('C6', "Perempuan")
							->setCellValue('D6', round(($count_p/count($laporan_jk)*100),2) . "%");

/********************End*******************/

/********************Usia*******************/
$t_usia = 0;
$awal	= 7;
$a		= 6;
foreach ($laporan_usia as $row) {
	$t_usia	= $t_usia + $row["USIA"];
}
foreach ($laporan_usia as $row) {
	$a++;
	$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('C'.$a, $row["range_usia"])
				->setCellValue('D'.$a, round(($row["USIA"]/$t_usia*100),2) . "%");
}
$objPHPExcel->setActiveSheetIndex(0)
		    ->mergeCells('A'.$awal.':A'.$a)->setCellValue('A7', "2")
		    ->mergeCells('B'.$awal.':B'.$a)->setCellValue('B7', "Kelompok Umur");
/********************End*******************/

/********************Pendidikan*******************/		
$t_pendidikan = 0;
$awal = $a;			
foreach ($laporan_pendidikan as $row) {
	$t_pendidikan	= $t_pendidikan + $row["PENDIDIKAN"];
}
foreach ($laporan_pendidikan as $row) {
	$a++;
	$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('C'.$a, $row["nm_pendidikan"])
		->setCellValue('D'.$a, round(($row["PENDIDIKAN"]/$t_pendidikan*100),2) . "%");
}
$objPHPExcel->setActiveSheetIndex(0)
    ->mergeCells('A'.($awal+1).':A'.($a))->setCellValue('A'.($awal+1), "3")
    ->mergeCells('B'.($awal+1).':B'.($a))->setCellValue('B'.($awal+1), "Pendidikan");
/********************End*******************/

/********************Jenis Kelamin*******************/	
$t_pekerjaan = 0;
$awal = $a;
foreach ($laporan_pekerjaan as $row) {
	$t_pekerjaan	= $t_pekerjaan + $row["PEKERJAAN"];
}
foreach ($laporan_pekerjaan as $row) {
	$a++;
	$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('C'.$a, $row["nm_pekerjaan"])
		->setCellValue('D'.$a, round(($row["PEKERJAAN"]/$t_pekerjaan*100),2) . "%");
}
$objPHPExcel->setActiveSheetIndex(0)
   ->mergeCells('A'.($awal+1).':A'.($a))->setCellValue('A'.($awal+1), "4")
    ->mergeCells('B'.($awal+1).':B'.($a))->setCellValue('B'.($awal+1), "Pekerjaan");
/********************End*******************/

/********************Frekuensi Urus Izin*******************/
$t_urus_izin = 0;
$awal = $a;
foreach ($laporan_izin as $row) {
	$t_urus_izin	= $t_urus_izin + $row["IZIN"];
}
foreach ($laporan_izin as $row) {
	$a++;
	$objPHPExcel->setActiveSheetIndex(0)
	->setCellValue('C'.$a, $row["urus_izin"] . " kali")
	->setCellValue('D'.$a, round(($row["IZIN"]/$t_urus_izin*100),2) . "%");
}
$objPHPExcel->setActiveSheetIndex(0)
    ->mergeCells('A'.($awal+1).':A'.($a))->setCellValue('A'.($awal+1), "5")
    ->mergeCells('B'.($awal+1).':B'.($a))->setCellValue('B'.($awal+1), "Frekuensi Urus Izin");
/********************End*******************/
	
// $objPHPExcel->getActiveSheet()->getStyle("A4:D".$a)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle("A4:D".$a)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Karakteristik_Responden');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Karakteristik_Responden.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
